package com.own.sign.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.own.sign.dto.SignReqDTO;
import com.own.sign.socket.WebCallback;
import com.own.sign.socket.WebSocketClientHandle;
import com.own.sign.utils.WebUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 加签入口
 *
 * 抽离出来的海关操作员卡加签名接口
 *
 * 需要读取插入服务器的读卡器的操作员卡（非法人卡）
 * 进行数据加签操作
 *@author liujiacheng
 */
@RestController
public class SignController {

    @Autowired
    private WebSocketClientHandle webSocketClientHandle;

    private static final Logger LOGGER = LoggerFactory.getLogger(SignController.class);

    /**
     * 测试请求接口
     */
    @RequestMapping(value = "test",method = RequestMethod.POST)
    public void test(String name, HttpServletRequest request, HttpServletResponse response){
        System.out.print(name);
        WebUtils.doSuccess(name,request,response);
    }

    /**
     * 加签调用方法
     */
    @RequestMapping(value = "sign",method = RequestMethod.POST)
    public void sign(@RequestBody final SignReqDTO signReqDTO,  HttpServletRequest request,  HttpServletResponse response) throws InterruptedException{

        LOGGER.info("入参-{}", signReqDTO);

        final Map<String,Object> signResuleMap = new HashMap<>();

//        Vector<Thread> threadVector = new Vector<>();
//
//        Thread childThread = new Thread(new Runnable() {
//            @Override
//            public void run() {
                webSocketClientHandle.send(signReqDTO.getKey(), signReqDTO.getMessage(), new WebCallback() {

                    @Override
                    public void message(String key, Object param) {
                        LOGGER.info("key-{}, message-{}", key, param);
                        String signValue = null;
                        if(!StringUtils.isEmpty(param)){
                            JSONObject jsonObject = JSON.parseObject(String.valueOf(param));
                            Integer id = jsonObject.getInteger("_id");
                            if(id != null && id == 1){
                                JSONObject args = jsonObject.getJSONObject("_args");
                                JSONArray data = args.getJSONArray("Data");
                                signValue = data.getString(0);
                                LOGGER.info("key-{}, signValue-{}", key, signValue);
                                signResuleMap.put("signValue",signValue);
                            }
                        }
                        // todo 放入处理队列
                        LOGGER.info("内部接收到的：{}",signResuleMap);
                    }

                    @Override
                    public void error(Object param) {
                        LOGGER.info("error-{}", param);
                    }
                });
//            }
//        });

//        threadVector.add(childThread);
//        childThread.start();
//
//        for (Thread thread:threadVector){
//            thread.join();
//        }

        Thread.sleep(3500);
        LOGGER.info("外部接收到的：{}",signResuleMap);

        Object signValue = signResuleMap.get("signValue");

        if(signValue!=null){
            WebUtils.doSuccess("加签成功",signValue,request,response);
        }else{
            WebUtils.doError("加签失败",request,response);
        }

    }
}
